﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using CNVP.Framework;
using CNVP.Framework.Utils;
using CNVP.Config;

namespace CNVP.JShop.UI
{
    public class AdminPage : LicensePage
    {
        public AdminPage()
        {
            base.Load += new EventHandler(this.AdminPage_Load);
        }

        /// <summary>
        /// 读取登录状态下Cookies中保存的管理员名
        /// </summary>
        public string CookieString
        {
            get
            {
                try
                {
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + ""];
                    if (Cookie != null)
                    {
                        return EncryptUtils.DecodeCookies(Cookie.Value);
                    }
                    else
                    {

                        return null;
                    }
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取管理员ID
        /// </summary>
        public int CookieAdminID
        {
            get
            {
                try
                {
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "AdminID"];
                    if (Cookie != null)
                    {
                        return Convert.ToInt32(EncryptUtils.DecodeCookies(Cookie.Value));
                    }
                    else
                    {

                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 获取管理员所属的角色
        /// </summary>
        public int CookieRoleID
        {
            get
            {
                try
                {
                    HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "RoleID"];
                    if (Cookie != null)
                    {
                        return Convert.ToInt32(EncryptUtils.DecodeCookies(Cookie.Value));
                    }
                    else
                    {

                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }


        /// <summary>
        /// 判断是否登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AdminPage_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.AddHeader("pragma", "no-cache");
            HttpContext.Current.Response.AddHeader("cache-control", "");
            HttpContext.Current.Response.CacheControl = "no-cache";

            HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + ""];
            HttpCookie Cookie_AdminID = Request.Cookies["" + UIConfig.CookiesName + "AdminID"];
            HttpCookie Cookie_RoleID = Request.Cookies["" + UIConfig.CookiesName + "RoleID"];
            if (Cookie == null || Cookie_AdminID == null || Cookie_RoleID == null)
            {
                MessageUtils.Write("<script>top.location.href=\"" + LicensePage.InstallDir + LicensePage.AdminDir + "/Login.aspx\"</script>");
            }
            else
            {
                AdminID = CookieAdminID;
                RoleID = CookieRoleID;
                AdminName = CookieString;
            }
        }
        /// <summary>
        /// 管理员退出
        /// </summary>
        protected void LoginOut()
        {
            HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + ""];
            HttpCookie Cookie_AdminID = Request.Cookies["" + UIConfig.CookiesName + "AdminID"];
            HttpCookie Cookie_RoleID = Request.Cookies["" + UIConfig.CookiesName + "RoleID"];
            if (Cookie != null && Cookie_AdminID != null && Cookie_RoleID != null)
            {
                Cookie.Expires = DateTime.Now.AddDays(-1);
                Cookie.Value = null;
                Response.Cookies.Add(Cookie);

                Cookie_AdminID.Expires = DateTime.Now.AddDays(-1);
                Cookie_AdminID.Value = null;
                Response.Cookies.Add(Cookie_AdminID);

                Cookie_RoleID.Expires = DateTime.Now.AddDays(-1);
                Cookie_RoleID.Value = null;
                Response.Cookies.Add(Cookie_RoleID);
            }
            Context.Session.Clear();
            Context.Session.Abandon();

            MessageUtils.Write("<script>top.location.href=\"" + LicensePage.InstallDir + LicensePage.AdminDir + "/Login.aspx\"</script>");
        }
    }
}